package com.lwlk.util.flow;

import java.util.function.Consumer;
import java.util.function.Function;

public class ConstRef<T> {

    private final T value;

    public ConstRef(final T value) {

        this.value = value;
    }

    public <R> ConstRef<R> map(final Function<T, R> trans) {

        return isPresent() ? new ConstRef<>(trans.apply(value)) : new ConstRef<>(null);
    }

    public <R> R flatmap(final Function<T, R> trans) {

        return isPresent() ? trans.apply(value) : null;
    }

    public boolean isPresent() {

        return value == null;
    }

    public void ifPresent(final Consumer<T> consumer) {

        if(isPresent()) consumer.accept(get());
    }

    public T get() {

        return value;
    }

    public T orElse(final T elseValue) {

        return isPresent() ? get() : elseValue;
    }
}

